namespace CIS
{
    using CISLib;
    using CISLib.UserControls;
    using System;
    using System.Drawing;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class UserCustomerSetPage3 : Page
    {
        protected Button btnBack;
        protected Button btnFromSelect;
        protected DropDownList ddlFromSelectCAT;
        protected DropDownList ddlFromSelectProvince;
        protected DataGrid FromListDg;
        protected System.Web.UI.WebControls.Image iCustomerInfo;
        protected Label lbFromUser;
        protected Label lbToUser;
        protected DataGrid ToListDg;
        protected Button tranBtn;

        private void btnBack_Click(object sender, EventArgs e)
        {
            base.Response.Write("<script>self.close();</script>");
        }

        private void btnFromSelect_Click(object sender, EventArgs e)
        {
            this.FromListDg.DataSource = ManageCustomer.GetCustomerBasicInfoViaUserId(new Guid(base.Request.QueryString["FromId"]), this.ddlFromSelectCAT.SelectedValue, this.ddlFromSelectProvince.SelectedValue);
            this.FromListDg.DataBind();
        }

        private void DgDataBind()
        {
            string strFromUserId = base.Request.QueryString["FromId"];
            string strToUserId = base.Request.QueryString["ToId"];
            this.FromListDg.DataSource = ManageCustomer.GetCustomerBasicInfoViaUserId(new Guid(strFromUserId));
            this.FromListDg.DataBind();
            this.ToListDg.DataSource = ManageCustomer.GetCustomerBasicInfoViaUserId(new Guid(strToUserId));
            this.ToListDg.DataBind();
        }

        protected Color GetFromColorViaCustomerId(object objCustomerId)
        {
            Guid userId = new Guid(base.Request.QueryString["FromId"]);
            SysUser user = ManageUser.GetUserViaId(userId);
            Province[] provinceAry = ManageProvince.GetProvinceAryViaUserId(userId);
            Cat[] catAry = ManageCAT.GetCATAryViaId(userId);
            Guid customerId = (Guid) objCustomerId;
            CustomerBasicInfo customerBasicInfo = ManageCustomer.GetCustomerBasicInfoViaId(customerId);
            bool isSameProvince = false;
            foreach (Province tmpP in provinceAry)
            {
                if (tmpP.ProvinceId.Equals(customerBasicInfo.RegionId))
                {
                    isSameProvince = true;
                    break;
                }
            }
            bool isSameCat = false;
            foreach (Cat tmpC in catAry)
            {
                if (tmpC.CatId.Equals(customerBasicInfo.CatId))
                {
                    isSameCat = true;
                    break;
                }
            }
            if (isSameCat && isSameProvince)
            {
                return Color.Black;
            }
            return Color.Red;
        }

        protected Color GetToColorViaCustomerId(object objCustomerId)
        {
            Guid userId = new Guid(base.Request.QueryString["ToId"]);
            SysUser user = ManageUser.GetUserViaId(userId);
            Province[] provinceAry = ManageProvince.GetProvinceAryViaUserId(userId);
            Cat[] catAry = ManageCAT.GetCATAryViaId(userId);
            Guid customerId = (Guid) objCustomerId;
            CustomerBasicInfo customerBasicInfo = ManageCustomer.GetCustomerBasicInfoViaId(customerId);
            bool isSameProvince = false;
            foreach (Province tmpP in provinceAry)
            {
                if (tmpP.ProvinceId.Equals(customerBasicInfo.RegionId))
                {
                    isSameProvince = true;
                    break;
                }
            }
            bool isSameCat = false;
            foreach (Cat tmpC in catAry)
            {
                if (tmpC.CatId.Equals(customerBasicInfo.CatId))
                {
                    isSameCat = true;
                    break;
                }
            }
            if (isSameCat && isSameProvince)
            {
                return Color.Black;
            }
            return Color.Red;
        }

        private void InitializeComponent()
        {
            this.tranBtn.Click += new EventHandler(this.tranBtn_Click);
            this.btnBack.Click += new EventHandler(this.btnBack_Click);
            this.btnFromSelect.Click += new EventHandler(this.btnFromSelect_Click);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            string strFromUserId = base.Request.QueryString["FromId"];
            string strToUserId = base.Request.QueryString["ToId"];
            SysUser fromUser = ManageUser.GetUserViaId(new Guid(strFromUserId));
            SysUser toUser = ManageUser.GetUserViaId(new Guid(strToUserId));
            this.lbFromUser.Text = fromUser.LoginName;
            this.lbToUser.Text = toUser.LoginName;
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.InitPage();
                this.DgDataBind();
                Province[] provinceAry = ManageProvince.GetProvinceAryViaUserId(new Guid(base.Request.QueryString["FromId"]));
                this.ddlFromSelectProvince.Items.Add(new ListItem("ALL", ""));
                for (int i = 0; i < provinceAry.Length; i++)
                {
                    ListItem tmpItem = new ListItem();
                    tmpItem.Text = provinceAry[i].ProvinceName;
                    tmpItem.Value = provinceAry[i].ProvinceId.ToString();
                    this.ddlFromSelectProvince.Items.Add(tmpItem);
                }
                Cat[] catAry = ManageCAT.GetCATAryViaId(new Guid(base.Request.QueryString["FromId"]));
                this.ddlFromSelectCAT.Items.Add(new ListItem("ALL", ""));
                for (int i = 0; i < catAry.Length; i++)
                {
                    ListItem tmpItem = new ListItem();
                    tmpItem.Text = catAry[i].CatName;
                    tmpItem.Value = catAry[i].CatId.ToString();
                    this.ddlFromSelectCAT.Items.Add(tmpItem);
                }
            }
        }

        private void tranBtn_Click(object sender, EventArgs e)
        {
            string strToUserId = base.Request.QueryString["ToId"];
            foreach (DataGridItem item in this.FromListDg.Items)
            {
                RepeaterCheckBox isDelChkTmp = item.FindControl("isDelChk") as RepeaterCheckBox;
                if ((isDelChkTmp != null) && isDelChkTmp.Checked)
                {
                    Guid customerId = new Guid(isDelChkTmp.Value);
                    ManageCustomer.UpdateCustomerMasterViaCustomerId(customerId, new Guid(strToUserId));
                }
            }
            this.DgDataBind();
        }
    }
}

